package net.esj.basic.core.view.tools;

import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import net.esj.basic.utils.Validators;

import org.apache.commons.lang.StringUtils;

public class ClientAgent {

	private String osVersion ;
	
	private String browserVersion;
	
	private String ip;
	
	private boolean mobile;

	public ClientAgent(HttpServletRequest req) {
		super();
		String agent = req.getHeader("user-agent");
        //System.out.println(agent);
		//StringTokenizer st = new StringTokenizer(agent, ";");
       // st.nextToken();
       
        //得到用户的浏览器名
        //String userbrowser = st.nextToken();
		if(!Validators.isEmpty(agent)){
			String userbrowser = agent;
			if(userbrowser.indexOf("MSIE")>0)
				browserVersion = "MSIE";    
			else if(userbrowser.indexOf("Firefox")>0) 
				browserVersion = "Firefox";    
			else if(userbrowser.indexOf("Chrome")>0) 
				browserVersion = "Chrome";    
			else if(userbrowser.indexOf("Safari")>0) 
				browserVersion = "Safari";    
			else if(userbrowser.indexOf("Camino")>0) 
				browserVersion = "Camino";    
			else if(userbrowser.indexOf("Konqueror")>0) 
				browserVersion = "Konqueror";   
			
			//得到用户的操作系统
			//String useros = st.nextToken();
			String useros = agent;
			if (useros.indexOf("NT 6.1") > 0) {
				osVersion = "Windows 7";
			} 
			else if (useros.indexOf("NT 6.0") > 0) {
				osVersion = "Windows Vista/Server 2008";
			} else if (useros.indexOf("NT 5.2") > 0) {
				osVersion = "Windows Server 2003";
			} else if (useros.indexOf("NT 5.1") > 0) {
				osVersion = "Windows XP";
			} else if (useros.indexOf("NT 5") > 0) {
				osVersion = "Windows 2000";
			} else if (useros.indexOf("NT 4") > 0) {
				osVersion = "Windows NT4";
			} else if (useros.indexOf("Me") > 0) {
				osVersion = "Windows Me";
			} else if (useros.indexOf("98") > 0) {
				osVersion = "Windows 98";
			} else if (useros.indexOf("95") > 0) {
				osVersion = "Windows 95";
			} else if (useros.indexOf("Mac") > 0) {
				osVersion = "Mac";
			} else if (useros.indexOf("Unix") > 0) {
				osVersion = "UNIX";
			} else if (useros.indexOf("Linux") > 0) {
				osVersion = "Linux";
			} else if (useros.indexOf("SunOS") > 0) {
				osVersion = "SunOS";
			}
			
		}

        ip = req.getHeader("X-Real-IP");
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getHeader("X-Forwarded-For");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getRemoteAddr();
        } else {
            int index = ip.indexOf(',');
            if (index != -1) {
                ip = ip.substring(0, index);
            }
        }
        mobile = HttpRequestDeviceUtils.isMobileDevice(req);
	}

	public String getOsVersion() {
		return osVersion;
	}

	public void setOsVersion(String osVersion) {
		this.osVersion = osVersion;
	}

	public String getBrowserVersion() {
		return browserVersion;
	}

	public void setBrowserVersion(String browserVersion) {
		this.browserVersion = browserVersion;
	}
	
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public boolean isMobile() {
		return mobile;
	}
	
}
